#pragma once 
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<pthread.h> 

#define BACKLOG 5


class TcpSever
{
  private:
    int port;
    int sock;
  public:
    TcpSever(int _port)
      :port(_port)
      ,sock(-1)
    {}
    void Init()
    {
      //创建，绑定，监听，链接
    }
    void Socket()
    {
     sock=socket(AF_INET,SOCK_STREAM,0);
     if(sock<0)
     {
       std::cout<<"创建套接字失败"<<std::endl;
       exit(1);
     }
    }
    void Bind()
    {
      struct sockaddr_in local;
      memset(&local,0,sizeof(local));
      local.sin_family=AF_INET;
      local.sin_port=htons(port);
      local.sin_addr.s_addr=htonl(INADDR_ANY);
      if(bind(sock,(struct sockaddr*)(&local),sizeof(local))<0)
      {
        std::cerr<<"bind fail"<<std::endl;
        exit(2);
      }
    }
    void Listen()
    {
      if(listen(sock,BACKLOG)<0)
      {
        std::cerr<<"listen error"<<std::endl;
      }
    }
    void SetSockOpt()
    {
      int opt=1;
      setsockopt(sock,SOL_SOCKET,SO_REUSEPORT,&opt,sizeof(opt));
    }
    int AccEpt()
    {
      struct sockaddr_in peer;
      socklen_t len=sizeof(peer);
      int s=accept(sock,(struct sockaddr*)(&peer),&len);
      if(s<0)
      {
        std::cerr<<"accept error"<<std::endl;
      }
      return s;
    }
};

